<?php

namespace app\index\controller;

use app\common\model\AchievementModel;
use app\common\model\UserModel;
use app\index\controller\IndexBase;
use app\index\model\Page;

/**
 *  成果审核操作
 * 
 * 时间：2020年4月25日20:29:17
 * 作者：夏立军
 * 
 */
class AchievementAudit extends IndexBase
{
    // 首页
    public function index()
    {
        $achievement = new AchievementModel();
        $achievement_list = $achievement->get_achievement_list_flag_to_2();

        $data = array();
        foreach ($achievement_list as $row) {
            // 获取中间表数据
            $achievement_classify_table_info = $achievement->get_achievement_classify_table_info_by_cid($row['id']);

            $table_name = $achievement_classify_table_info['table_name'];

            $row['achievement_count'] = $achievement->get_achievement_classify_table_other_info_by_tableName_count($table_name);
            $data[] = $row;
        }


        return view('index', [
            'achievement_list' => $data,
        ]);
    }

    // 详情
    public function details()
    {

        $uid = session('index_login')->id;
        $cid = input('get.cid/d', null);
        if (is_null($cid)) {
            return redirect_index('未传入CID', url('AchievementAudit/index') . '?uid=' . $uid);
        }
        // 载入成果模型
        $achievement = new AchievementModel();
        $classify_info = $achievement->get_classify_info_by_id($cid);
        $classify_table_info = $achievement->get_achievement_classify_table_info_by_cid($cid);
        if (!$classify_table_info) {
            return redirect_index('数据异常，请联系管理员', url('AchievementAudit/index') . '?uid=' . $uid);
        }
        // 获取表名
        $table_name = $classify_table_info['table_name'];
        // 载入用户模型
        $user = new UserModel();
        // 获取审核名单的每页数量
        $page_size = config('ams_config.index_achievement_audit_page_size');
        $page = new Page();
        $page_res = $page->getPageRes($user->get_user_count(), $page_size, url('AchievementAudit/details') . "?cid=" . $cid);

        $user_list = $user->user_list_page($page_res['offset'], $page_res['size']);

        foreach ($user_list as $row) {
            $row['count'] = count(
                $achievement->get_achievement_classify_table_other_info_and_count_by_tableName_and_uid($table_name, $row['id'])
            );
            $data[] = $row;
        }

        return view("details", [
            'user_list' => $data,
            'page_res' => $page_res,
            'classify_info' => $classify_info,
            'cid' => $cid,
            'uid' => $uid,
        ]);
    }

    // 审核
    public function audit()
    {
        $cid = input('get.cid/d', null);
        $uid = input('get.uid/d', null);

        // 实例化一个成果模型
        $achievement = new AchievementModel();

        // 获取中间表的内容
        $classify_table_info = $achievement->get_achievement_classify_table_info_by_cid($cid);

        // 获取已经提交的用户ID
        $classify_table_other_data = $achievement->get_achievement_classify_table_other_info_and_count_by_tableName_and_uid($classify_table_info['table_name'], $uid);

        // 数据类型下拉列表数据
        $classify_type_list = config('ams_config.classify_type_list');

        return view('audit', [
            'classify_table_info' => $classify_table_info,
            'classify_table_other_data' => $classify_table_other_data,
            'classify_type_list' => $classify_type_list,
            'cid' => $cid,
            'uid' => $uid,
        ]);
    }

    // 审核通过
    public function through()
    {
        if (request()->post()) {
            $id = input('post.id/d', null);
            $cid = input('post.cid/d', null);
            $uid = input('post.uid/d', null);

            if (is_null($id) || is_null($cid) || is_null($uid)) {
                return json(['code' => 1, 'msg' => '请求参数丢失']);
            }

            // 实例化一个成果模型
            $achievement = new AchievementModel();

            // 获取中间表的内容
            $classify_table_info = $achievement->get_achievement_classify_table_info_by_cid($cid);

            $table_name = $classify_table_info['table_name'];

            $achievement_classify_table_res =
                $achievement->set_achievement_classify_table_status_by_tableName_and_uid_and_id(
                    $table_name,
                    $uid,
                    $id,
                    ['status' => 4, 'audit_id' => session('index_login')->id]
                );

            if (!$achievement_classify_table_res) {
                return json(['code' => 2, 'msg' => '状态修改失败，请检查']);
            }

            return json(['code' => 0, 'msg' => '审核已通过']);
        }
        return json(['code' => 99, 'msg' => '请求方式错误']);
    }

    // 审核驳回
    public function rejected()
    {
        if (request()->post()) {
            $id = input('post.id/d', null);
            $cid = input('post.cid/d', null);
            $uid = input('post.uid/d', null);
            $note = input('post.note/s', null);

            if (is_null($id) || is_null($cid) || is_null($uid)) {
                return json(['code' => 1, 'msg' => '请求参数丢失']);
            }

            $note = (is_null($note)) ? '无' : $note;

            // 实例化一个成果模型
            $achievement = new AchievementModel();

            // 获取中间表的内容
            $classify_table_info = $achievement->get_achievement_classify_table_info_by_cid($cid);

            $table_name = $classify_table_info['table_name'];

            $achievement_classify_table_res =
                $achievement->set_achievement_classify_table_status_by_tableName_and_uid_and_id(
                    $table_name,
                    $uid,
                    $id,
                    ['status' => 3, 'note' => $note, 'audit_id' => session('index_login')->id]
                );

            if (!$achievement_classify_table_res) {
                return json(['code' => 2, 'msg' => '状态修改失败，请检查']);
            }

            return json(['code' => 0, 'msg' => '审核已驳回']);
        }
        return json(['code' => 99, 'msg' => '请求方式错误']);
    }

    // 审核详情页面
    public function audit_details()
    {
        $uuid = session('index_login')->id;
        $cid = input('get.cid/d', null);
        $uid = input('get.uid/d', null);
        $status = input('get.status/d', null);
        if (is_null($cid) || is_null($uid)) {
            return redirect_index('参数丢失', url('AchievementAudit/index') . '?uid=' . $uuid);
        }

        // 获取表名
        // 实例化一个成果模型
        $achievement = new AchievementModel();
        // 获取中间表的内容
        $classify_table_info = $achievement->get_achievement_classify_table_info_by_cid($cid);

        $audit_id = session('index_login')->id;
        if (is_null($status)) {
            // 获取已经提交的用户ID
            $classify_table_other_data = $achievement->get_classify_table_other_info_by_tableName_and_uid_and_auditId($classify_table_info['table_name'], $uid, $audit_id);
        } else {
            $classify_table_other_data = $achievement->get_classify_table_other_info_by_tableName_and_uid_and_auditId_and_status($classify_table_info['table_name'], $uid, $audit_id, $status);
        }
        // 数据类型下拉列表数据
        $classify_type_list = config('ams_config.classify_type_list');

        return view('audit_details', [
            'cid' => $cid,
            'uid' => $uid,
            'status' => $status,
            'classify_table_other_data' => $classify_table_other_data,
            'classify_type_list' => $classify_type_list,
            'classify_table_info' => $classify_table_info
        ]);
    }

    // 设置状态
    public function set_status()
    {
        $pid = input('post.pid/d', null);
        $id = input('post.id/d', null);
        $status = input('post.status/d', null);
        $note = input('post.note/s', '');

        if (is_null($pid) || is_null($id) || is_null($status)) {
            return json(['code' => 1, 'msg' => '参数丢失']);
        }

        $achievement = new AchievementModel();
        $classify_table_info = $achievement->get_achievement_classify_table_info_by_id($pid);
        if (!$classify_table_info) {
            return json(['code' => 2, 'msg' => '数据异常']);
        }

        $table_name = $classify_table_info['table_name'];
        $res = $achievement->set_achievement_info($table_name, $id, ['status' => $status, 'note' => $note]);
        if (!$res) {
            return json(['code' => 3, 'msg' => '设置失败']);
        }
        return json(['code' => 0, 'msg' => '设置完成']);
    }
}
